package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.domain.BizSearchKeyword;
import cn.sfrog.project.biz.admin.domain.vo.BizSearchKeywordFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizSearchKeywordQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizSearchKeywordVO;
import cn.sfrog.project.biz.admin.mapper.BizSearchKeywordMapper;
import cn.sfrog.project.biz.admin.service.IBizSearchKeywordService;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 搜索关键字Service业务层处理
 */
@Service
public class BizSearchKeywordServiceImpl extends ServiceImpl<BizSearchKeywordMapper, BizSearchKeyword> implements IBizSearchKeywordService {
    @Resource
    private BizSearchKeywordMapper bizSearchKeywordMapper;

    /**
     * 查询搜索关键字
     *
     * @param id 搜索关键字主键
     * @return 搜索关键字
     */
    @Override
    public BizSearchKeywordVO selectBizSearchKeywordById(Long id) {
        return bizSearchKeywordMapper.selectBizSearchKeywordById(id);
    }

    /**
     * 查询搜索关键字列表
     *
     * @param query 搜索关键字
     * @return 搜索关键字
     */
    @Override
    public List<BizSearchKeywordVO> selectBizSearchKeywordList(BizSearchKeywordQueryVO query) {
        return bizSearchKeywordMapper.selectBizSearchKeywordList(query);
    }

    /**
     * 新增搜索关键字
     *
     * @param form 搜索关键字
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizSearchKeyword(BizSearchKeywordFormVO form) {
        BizSearchKeyword bizSearchKeyword = new BizSearchKeyword();
        BeanUtils.copyProperties(form, bizSearchKeyword);
        bizSearchKeyword.setCreateTime(new Date());
        bizSearchKeyword.setCreateBy(SecurityUtils.getUsername());
        bizSearchKeyword.setUpdateTime(new Date());
        bizSearchKeyword.setUpdateBy(SecurityUtils.getUsername());
        return bizSearchKeyword.insert();
    }

    /**
     * 修改搜索关键字
     *
     * @param form 搜索关键字
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizSearchKeyword(BizSearchKeywordFormVO form) {
        BizSearchKeyword bizSearchKeyword = new BizSearchKeyword();
        BeanUtils.copyProperties(form, bizSearchKeyword);
        bizSearchKeyword.setUpdateTime(new Date());
        bizSearchKeyword.setUpdateBy(SecurityUtils.getUsername());
        return bizSearchKeyword.updateById();
    }

    /**
     * 批量删除搜索关键字
     *
     * @param ids 需要删除的搜索关键字主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizSearchKeywordByIds(Long[] ids) {
            return BizSearchKeyword.DAO.delete(Wrappers.lambdaQuery(BizSearchKeyword.class).in(BizSearchKeyword::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
//        return BizSearchKeyword.DAO.update(Wrappers.lambdaUpdate(BizSearchKeyword.class).in(BizSearchKeyword::getId, (Object[]) vo.getIds()).set(BizSearchKeyword::getStatus, vo.getStatus()));
        return true;
    }

    @Override
    public List<String> selectAppIndexHotSearchKeyword() {
        return bizSearchKeywordMapper.selectAppIndexHotSearchKeyword();
    }
}
